using Simple.Contracts.Contracts;
using Simple.Contracts.Models;
using Simple.Contracts.Models.Common;
using Simple.Repository;
using Simple.Repository.SimpleEntitys;
using Simple.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Services.Services
{
    /// <summary>
    /// 省市区服务实现
    /// </summary>
    public class SysPlacesService : BaseService, ISysPlacesService
    {
        public SysPlacesService(FreeSqlCloud freeSqlCloud, CurrentAccount currentAccount) : base(freeSqlCloud, currentAccount)
        {
        }

        /// <summary>
        /// 获取省市区列表
        /// </summary>
        /// <param name="input">获取省市区列表输入参数</param>
        /// <returns>省市区列表</returns>
        public async Task<Result<List<SysPlacesDto>>> GetList(SysPlacesListInput input)
        {
            try
            {
                if (input.GetAllLevels)
                {
                    // 获取所有层级的数据并构建成树状结构
                    var allPlaces = await _simpleFreeSql.Select<SysPlaces>().ToListAsync();
                    var tree = BuildTree(allPlaces, input.ParentId);
                    return Result<List<SysPlacesDto>>.Success(tree);
                }
                else
                {
                    // 只获取指定父级ID的直接子节点
                    var places = await _simpleFreeSql.Select<SysPlaces>()
                        .Where(x => x.ParentId == input.ParentId)
                        .OrderBy(x => x.Id)
                        .ToListAsync();

                    var dtos = places.Select(x => new SysPlacesDto
                    {
                        Id = x.Id,
                        ParentId = x.ParentId,
                        Cname = x.Cname,
                        Ctype = x.Ctype,
                        Children = new List<SysPlacesDto>()
                    }).ToList();

                    return Result<List<SysPlacesDto>>.Success(dtos);
                }
            }
            catch (Exception ex)
            {
                return Result<List<SysPlacesDto>>.FriendlyReminder("获取省市区数据失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 构建树状结构
        /// </summary>
        /// <param name="allPlaces">所有地区数据</param>
        /// <param name="parentId">父级ID</param>
        /// <returns>树状结构数据</returns>
        private List<SysPlacesDto> BuildTree(List<SysPlaces> allPlaces, ushort parentId)
        {
            var tree = new List<SysPlacesDto>();
            var places = allPlaces.Where(x => x.ParentId == parentId).OrderBy(x => x.Id).ToList();

            foreach (var place in places)
            {
                var dto = new SysPlacesDto
                {
                    Id = place.Id,
                    ParentId = place.ParentId,
                    Cname = place.Cname,
                    Ctype = place.Ctype,
                    Children = BuildTree(allPlaces, place.Id)
                };
                tree.Add(dto);
            }

            return tree;
        }
    }
}